package com.dingding.service.user.core.login.service.impl;

import java.math.BigDecimal;
import java.util.Date;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.dingding.common.core.beans.UserLogin;
import com.dingding.common.core.beans.Useraccount;
import com.dingding.common.core.beans.Userdetail;
import com.dingding.common.core.beans.Userintegral;
import com.dingding.common.core.beans.Vehicleinfo;
import com.dingding.common.core.util.ToolUtil;
import com.dingding.service.user.core.dao.IUserAccountDao;
import com.dingding.service.user.core.dao.IUserDetailDao;
import com.dingding.service.user.core.dao.IUserIntegralDao;
import com.dingding.service.user.core.dao.IUserLoginDao;
import com.dingding.service.user.core.dao.IVehicleInfoDao;
import com.dingding.service.user.core.login.service.IUserLoginService;

/**
 * 
 * @ClassName: IUserLoginService 
 * @Description: 用户登录Service
 * @author CHEHAUNBO 
 * @date 2016年7月25日 下午4:47:24
 * @since V2.3.2
 * 
 */
@Component("userLoginService")
public class UserLoginServiceImpl implements IUserLoginService {

	private Logger log = Logger.getLogger(UserLoginServiceImpl.class);

	/**
	 * 用户登录
	 */
	@Resource(name = "userLoginDao")
	private IUserLoginDao userLoginDao;

	/**
	 * 用户详情
	 */
	@Resource(name = "userDetailDao")
	private IUserDetailDao userDetailDao;

	/**
	 * 用户账户
	 */
	@Resource(name = "userAccountDao")
	private IUserAccountDao userAccountDao;

	/**
	 * 用户积分
	 */
	@Resource(name = "userIntegralDao")
	private IUserIntegralDao userIntegralDao;

	/**
	 * 用户车辆信息
	 */
	@Resource(name = "vehicleInfoDao")
	private IVehicleInfoDao vehicleInfoDao;

	/**
	 * 
	 * 根据手机号获取用户登录信息
	 * 
	 * @Title: getUserLoginInfoByTelPhone 
	 * @param userId 用户ID
	 * @return UserLogin 获取用户登录信息
	 * @author CHEHAUNBO
	 * @since V2.3.2
	 * 
	 */
	@Override
	public UserLogin getUserLoginInfoByTelPhone(String userTelPhone) {

		try {
			return userLoginDao.getUserLoginByProperty("userName", userTelPhone);
		} catch (Exception e) {
			log.error("根据用户手机号获取用户信息出现异常！", e);
		}
		return null;
	}

	/**
	 * 
	 * 用户注册，系统内部调用
	 * 
	 * 描述：此方法只能在系统内使用，例如：后台爬取数据自动注册用户
	 * 
	 * @Title: userLogin 
	 * @param userTelPhone 用户手机号
	 * @return boolean true 成功 false 失败
	 * @author CHEHAUNBO
	 * 
	 */
	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public boolean userRegister(String userTelPhone) {

		if (ToolUtil.isBlank(userTelPhone)) {
			log.error("用户注册出现异常，原因手机号为空！");
			return false;
		}

		// 验证用户是否存在
		UserLogin userLogin = userLoginDao.getUserLoginByProperty("userName", userTelPhone);
		if (userLogin != null) {
			return true;
		}

		return addUser(userTelPhone);

	}

	/**
	 * 
	 * 添加用户
	 * 
	 * @Title: addUser 
	 * @param userTelphone 用户手机号
	 * @author CHEHAUNBO
	 * @since V2.3.2
	 * 
	 */
	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public boolean addUser(String userTelphone) {
		return this.addUser(userTelphone, null);
	}

	/**
	 * 
	 * 添加用户
	 * 
	 * @Title: addUser 
	 * @param userTelphone 用户手机号
	 * @param weChartId 微信ID
	 * @author CHEHAUNBO
	 * @since V2.3.2
	 * 
	 */
	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public boolean addUser(String userTelphone, String weChartId) {

		try {

			String loginId = ToolUtil.getUid();
			// 保存用户登录信息
			userLoginDao.save(createUserLogin(loginId, userTelphone, weChartId));
			// 保存用户详细信息
			userDetailDao.save(createUserDetail(userTelphone, loginId));
			// 保存用户账户信息
			userAccountDao.save(createUserAccount(loginId));
			// 保存用户积分信息
			userIntegralDao.save(createUserIntegral(loginId));
			// 保存用户车辆信息
			vehicleInfoDao.save(createVehicleInfo(loginId));

			return true;

		} catch (Exception e) {
			log.error("保存用户信息出现异常！", e);
		}

		return false;

	}

	private UserLogin createUserLogin(String loginId, String userTelPhone, String weChatId) {

		UserLogin userlogin = new UserLogin();
		// 为用户登录表设置主键
		userlogin.setUid(loginId);
		// 用户登录手机号
		userlogin.setUserName(userTelPhone);
		// 登录状态
		userlogin.setUserStatus(2);
		// 用户角色1：车主 2：乘客
		userlogin.setUserRole(2);
		// 是否设置过提现密码
		userlogin.setIsDeposit(1);
		// 是否内部员工 1:不是 2：是
		userlogin.setIsEmployee(1);
		// 注册时间
		userlogin.setCreateTime(new Date());
		// 下单数默认为0
		userlogin.setPublishNumber(0);
		// 取消订单数默认为0
		userlogin.setCancelNumber(0);
		// 提现次数
		userlogin.setDepositNumber(0);
		// 微信ID
		userlogin.setWeChatId(weChatId);
		// 是否参加活动
		userlogin.setIsAttendActivity(2);

		return userlogin;

	}

	private Userdetail createUserDetail(String userTelPhone, String userLoginId) {

		Userdetail userdetail = new Userdetail();
		// 为用户详情表设置主键
		userdetail.setUdId(ToolUtil.getUid());
		// 用户登录ID
		userdetail.setLoginId(userLoginId);
		// 用户昵称
		userdetail.setNickName("叮叮小伙伴");
		// 用户性别
		userdetail.setSex(1);
		// 用户手机号码
		userdetail.setTellphone(userTelPhone);
		// 用户头像设置
		userdetail.setUserAvatar("header_2x.png");
		// 总搭乘
		userdetail.setTotalTake(new BigDecimal(0));
		// 总接程
		userdetail.setTotalWeldingProcess(new BigDecimal(0));
		// 总搭乘次数
		userdetail.setTotalTakeNumber(0);
		// 总接乘次数
		userdetail.setTotalWeldingProcessNumber(0);
		// 用户信誉等级新用户默认5星
		userdetail.setUserLevel(new BigDecimal(5));

		return userdetail;

	}

	private Useraccount createUserAccount(String userLoginId) {

		Useraccount useraccount = new Useraccount();
		// 为用户账户表设置主键
		useraccount.setUaId(ToolUtil.getUid());
		// 用户登录ID
		useraccount.setLoginId(userLoginId);
		// 账户余额
		useraccount.setCurrencyAmount(new BigDecimal(0));
		// 账户状态
		useraccount.setStatus(1);
		// 累计收入
		useraccount.setTotalIncome(new BigDecimal(0));
		// 累计提现
		useraccount.setTotalDeposit(new BigDecimal(0));
		// 累计支出
		useraccount.setTotalExpend(new BigDecimal(0));

		return useraccount;
	}

	private Userintegral createUserIntegral(String userLoginId) {

		Userintegral userintegral = new Userintegral();
		// 为用户账户表设置主键
		userintegral.setUsId(ToolUtil.getUid());
		// 用户登录ID
		userintegral.setLoginId(userLoginId);
		// 我的积分
		userintegral.setUserIntegral(new BigDecimal(0));
		// 账户状态
		userintegral.setUiStatus(1);

		return userintegral;

	}

	private Vehicleinfo createVehicleInfo(String loginId) {

		Vehicleinfo vehicleinfo = new Vehicleinfo();
		// viid
		vehicleinfo.setUvId(ToolUtil.getUid());
		// loginId
		vehicleinfo.setLoginId(loginId);
		// 认证时间
		vehicleinfo.setCreateTime(new Date());
		// 认证状态 0:保留
		vehicleinfo.setVstatus(0);
		// 是否认证
		vehicleinfo.setIsAuth(1);

		return vehicleinfo;

	}

	/**
	 * @Description:通过手机号获取用户的id
	 * @author fengshuonan
	 * @since 2.3.2
	 */
	@Override
	@Transactional(readOnly = true)
	public String getUserIdByNumber(String tellphone) {

		if (ToolUtil.isBlank(tellphone)) {
			return null;
		}

		UserLogin login = this.getUserLoginInfoByTelPhone(tellphone);

		if (login != null) {
			return login.getUid();
		}

		return null;
	}

	/**
	 * @Description:同步订单根据发单类型，和发单人的手机号，修改发单人的乘客角色
	 * @param publisherType 发单类型（1:司机发单   2:乘客发单）
	 * @param tellphone 电话号码
	 * @author fengshuonan
	 * @since 2.3.2
	 */
	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public boolean changeSyncorderPublisherRole(Integer publisherType, String tellphone) {

		if (ToolUtil.isBlank(tellphone)) {
			log.error("同步订单根据发单类型，和发单人的手机号，修改发单人的乘客角色错误，手机号为空！UserLoginServiceImpl:changeSyncorderPublisherRole()");
			return false;
		}

		UserLogin userlogin = this.getUserLoginInfoByTelPhone(tellphone);

		userlogin.setIsDeposit(2);// 设置为是内部员工

		// 如果是司机发单
		if (publisherType == 1) {
			userlogin.setUserRole(1); // 设置为司机角色
		}

		return true;
	}

}
